<template>
    <el-select v-model="valueTitle" :value="valueTitle" :clearable="clearable" @clear="clearHandle" :disabled="!clickable" placeholder="请选择所属目录">
        <el-option :value="valueTitle" :label="valueTitle" class="options">
            <el-tree
                id="tree-option"
                ref="selectTree"
                :accordion="accordion"
                :data="options"
                :props="props"
                :node-key="props.value"
                :default-expanded-keys="defaultExpandedKey"
                @node-click="handleNodeClick">
            </el-tree>
        </el-option>
    </el-select>
</template>

<script>
import {reactive, ref, toRefs} from '@vue/reactivity'
import {getCurrentInstance, nextTick, onMounted} from "vue"
import {watch} from "@vue/runtime-core";

export default {
    name: "My-Tree-Select",
    props: {
        // 配置项,el-tree的配置项要叫props，这不是脑残设计吗
        props: {
            type: Object,
            default: () => {
                return {
                    value: 'id',             // ID字段名
                    label: 'title',         // 显示名称
                    children: 'children'    // 子级字段名
                }
            }
        },
        // 选项列表数据(树形结构的对象数组)
        options: {type: Array, default: () => []},
        // 初始值
        value: {type: String, default: null},
        // 可清空选项
        clearable: {type: Boolean, default: true},
        // 自动收起
        accordion: {type: Boolean, default: false},
        // 可点击
        clickable: {type: Boolean, default: false}
    },

    setup(props, context) {
        const selectData = reactive({
            valueId: null,
            valueTitle: '',
            defaultExpandedKey: []
        })

        const selectTree = ref(null)

        // 加载完后执行
        onMounted(() => {
            console.log('My-Tree-Select->onMounted->props:')
            console.log(props)
            // 先给valueId赋值
            selectData.valueId = props.value
            initHandle()
        })

        // 初始化值
        const initHandle = () => {
            console.log('My-Tree-Select->initHandle->options:')
            console.log(props.options)
            if (selectData.valueId) {
                /*
                    valueId
                    0：说明是顶级目录添加，目录树这边不进行任何操作
                    非0：正常书签/目录的编辑/添加
                 */
                if (selectData.valueId !== '0') {
                    console.log('My-Tree-Select->initHandle->valueId:')
                    console.log(selectData.valueId)
                    console.log('My-Tree-Select->initHandle->selectTree:')
                    console.log(selectTree)
                    console.log(selectTree.value)
                    console.log(selectTree.value.getNode(selectData.valueId).data[props.props.label])
                    console.log(selectTree.value.data)
                    selectData.valueTitle = selectTree.value.getNode(selectData.valueId).data[props.props.label]
                    // selectData.valueTitle = selectTree.getNode(selectData.valueId).data[props.props.lable]
                    selectTree.value.setCurrentKey(selectData.valueId)
                    selectData.defaultExpandedKey = [selectData.valueId]

                    initScroll()
                } else {
                    selectData.valueTitle = ''
                }
            }
        }

        // 初始化滚动条
        const initScroll = () => {
            nextTick(() => {
                let scrollWrap = document.querySelectorAll('.el-scrollbar .el-select-dropdown__wrap')[0]
                let scrollBar = document.querySelectorAll('.el-scrollbar .el-scrollbar__bar')
                scrollWrap.style.cssText = 'margin: 0px; max-height: none; overflow: hidden;'
                scrollBar.forEach(ele => ele.style.width = 0)
            })
        }

        // 切换选项
        const handleNodeClick = (node) => {
            console.log('My-Tree-Select->nodeClick->node')
            console.log(node)
            selectData.valueTitle = node[props.props.label]
            selectData.valueId = node[props.props.value]
            context.emit('refreshPid', selectData.valueId)
            selectData.defaultExpandedKey = []
        }

        // 清除选中
        const clearHandle = () => {
            selectData.valueTitle = ''
            selectData.valueId = null
            selectData.defaultExpandedKey = []
            clearSelected()
            context.emit('refreshPid', null)
        }

        // 清除选中样式
        const clearSelected = () => {
            let allNode = document.querySelectorAll('#tree-option .el-tree-node')
            allNode.forEach((element) => element.classList.remove('is-current'))
        }

        watch(
            () => props.value,
            (v) => {
                if (v) {
                    nextTick(() => {
                        console.log('valueId change:')
                        console.log(props.value)
                        selectData.valueId = props.value
                        initHandle()
                    })
                }
            }
        )

        return {
            ...toRefs(selectData),
            selectTree,
            handleNodeClick,
            clearHandle
        }
    }
}
</script>

<style scoped>
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
    height: auto;
    max-height: 274px;
    padding: 0;
    overflow: hidden;
    overflow-y: auto;
}

.el-select-dropdown__item.selected {
    font-weight: normal;
}

ul li >>> .el-tree .el-tree-node__content {
    height: auto;
    padding: 0 20px;
}

.el-tree-node__label {
    font-weight: normal;
}

.el-tree >>> .is-current .el-tree-node__label {
    color: #409EFF;
    font-weight: 700;
}

.el-tree >>> .is-current .el-tree-node__children .el-tree-node__label {
    color: #606266;
    font-weight: normal;
}
</style>
